home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / pstools / pscal.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.5 KB  |  205 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *      pscal - 
  19.  *              Make a calendar with an image.
  20.  *      
  21.  *                              Paul Haeberli - 1993
  22.  *
  23.  *
  24.  */
  25. #include <stdio.h>
  26. #include <math.h>
  27. #include "image.h"
  28.  
  29. #define DRAWBORDER    (0)
  30.  
  31. #define BARDY         (0.2)
  32. #define TOPY        ((11.0-BARDY)/2.0)
  33. #define BORDER         (8.5/16)
  34.  
  35. #define BOXDY         ((TOPY-BORDER)/6.0)
  36. #define BOXDX        ((8.5-2*BORDER)/7.0)
  37.  
  38. #define IXORG        (BORDER)
  39. #define IYORG        (TOPY+0.8)
  40. #define IXSIZE        (8.5-2*BORDER)
  41. #define IYSIZE        (11.0-BORDER-IYORG)
  42.  
  43. char *monthnames[] = {  "January", "February", "March", "April",
  44.             "May", "June", "July", "August",
  45.             "September", "October", "November", "December" };
  46.  
  47. char *daynames[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  48.  
  49. char monlength[] = { 0,
  50.             31, 29, 31, 30,
  51.             31, 30, 31, 31,
  52.             30, 31, 30, 31 };
  53.  
  54. FILE *outf;
  55.  
  56. main(argc, argv)
  57. char *argv[];
  58. {
  59.     if(argc<4) {
  60.     fprintf(stderr,"usage: pscal month year image.rgb\n");
  61.     exit(1);
  62.     }
  63.     outf = fopen("/usr/tmp/PSCALTEMP.ps","w");
  64.     if(!outf) {
  65.     fprintf(stderr,"pscal: can't open outputfile /usr/tmp/PSCALTEMP.ps\n");
  66.     exit(1);
  67.     }
  68.     drawcal(atoi(argv[1]),atoi(argv[2]),argv[3]);
  69.     fclose(outf);
  70.     system("lp < /usr/tmp/PSCALTEMP.ps");
  71.     system("rm /usr/tmp/PSCALTEMP.ps");
  72. }
  73.  
  74. IMAGE *inimage;
  75.  
  76. psbwgetrow(buf,y)
  77. short *buf;
  78. int y;
  79. {
  80.     getbwrow(inimage,buf,inimage->ysize-1-y);
  81. }
  82.  
  83. int drawcal(mon,year,iname)
  84. int mon, year;
  85. char *iname;
  86. {
  87.     int i, c;
  88.     float ycoord;
  89.     char tempstr[30];
  90.     float xstart;
  91.     int ixsize, iysize;
  92.     float iaspect, paspect;
  93.     float pxorg, pyorg;
  94.     float pxsize, pysize;
  95.  
  96.     xstart = (8.5-7*BOXDX)/2.0;
  97.  
  98.     beginps(outf,8.5/11.0,0.0,8.5,0.0,11.0);
  99.     psprintcortab();
  100.  
  101.     if(iname) {
  102.     sizeofimage(iname,&ixsize,&iysize);
  103.     paspect = (float)IXSIZE/IYSIZE;
  104.     iaspect = (float)ixsize/iysize;
  105.     if(iaspect>paspect) {
  106.         pxsize = IXSIZE;
  107.         pysize = (iysize*IXSIZE)/(float)ixsize;
  108.         pxorg = IXORG;
  109.         pyorg = IYORG+(IYSIZE-pysize)/2.0;
  110.     } else {
  111.         pysize = IYSIZE;
  112.         pxsize = (ixsize*IYSIZE)/(float)iysize;
  113.         pyorg = IYORG;
  114.         pxorg = IXORG+(IXSIZE-pxsize)/2.0;
  115.        }
  116.     psgsave();
  117.     pstranslate(pxorg,pyorg);
  118.     psscale(pxsize,pysize);
  119.     psgrey(0.0);
  120.      inimage = iopen(iname,"r");
  121.         if(!inimage) {
  122.         fprintf(stderr,"pscal: can't open input image file %s\n",iname);
  123.         exit(1);
  124.         }
  125.         tobwps(outf,psbwgetrow,0,8,ixsize,iysize);
  126.     pslinewidth(0.0);
  127.     if(DRAWBORDER)
  128.         psrect(0.0,0.0,1.0,1.0);
  129.     iclose(inimage);
  130.     psgrestore();
  131.     }
  132.  
  133.     pssetfont("Times-Roman",0.3);
  134.     psgrey(0.0);
  135.  
  136.     sprintf(tempstr,"%s %u",monthnames[mon-1],year);
  137.     psmoveto(xstart,TOPY+0.3);
  138.     psshow(tempstr);
  139.  
  140.     pssetfont("Times-Italic",0.20);
  141.     for (i=0; i<7; i++){
  142.     psgrey(0.0);
  143.         psrectf(xstart+BOXDX*i,TOPY,xstart+BOXDX*(i+1),TOPY+BARDY);
  144.     psgrey(1.0);
  145.     psmoveto(xstart+BOXDX/2.0+i*BOXDX,TOPY+0.05);
  146.         pscentershow(daynames[i]);
  147.     }
  148.  
  149.     c = dayofweek(mon, year);
  150.     ycoord = TOPY;
  151.     pslinewidth(0.0);
  152.     pssetfont("Times-Roman",0.15);
  153.     psgrey(0.0);
  154.     for (i=1; i<=monlength[mon]; i++) {
  155.         psmoveto(xstart+BOXDX*c+0.03,ycoord-0.15);
  156.         sprintf(tempstr,"%2d",i); 
  157.     psshow(tempstr);
  158.         psrect(xstart+BOXDX*c,ycoord,xstart+BOXDX*(c+1),ycoord-BOXDY);
  159.  
  160.     /* if finished with this line, drop down to next */
  161.         if (++c == 7) {
  162.             c = 0;
  163.             ycoord -= BOXDY;
  164.         }
  165.     }
  166.     endps();
  167. }
  168.  
  169. dayofweek(m, y)
  170. {
  171.     int d, i;
  172.  
  173.     d = jan1(y);
  174.  
  175.     if (((jan1(y+1)+7-d)%7) == 1)
  176.         monlength[2] = 28;
  177.     else
  178.         monlength[2] = 29;
  179.  
  180.     for (i=1; i<m; i++)
  181.         d += monlength[i];
  182.  
  183.     return d%7;
  184. }
  185.  
  186. /*
  187.  *      return day of the week
  188.  *      of jan 1 of given year
  189.  */
  190. jan1(y)
  191. int y;
  192. {
  193.     int d;
  194.  
  195.     d = y + 4 + (y+3)/4;
  196.     if (y > 1752) {
  197.         d += 3;
  198.         if (y > 1800) {
  199.             d -= (y-1701)/100;
  200.             d += (y-1601)/400;
  201.         }
  202.     }
  203.     return d%7;
  204. }
  205.